package com.xt.mvcpilot.controller.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


import com.xt.mvcpilot.controller.UserController;
import com.xt.mvcpilot.service.UserService;
import com.xt.mvcpilot.data.User;

@Controller
@RequestMapping("/users")
public class UserControllerImpl extends AppBasicControllerImpl implements UserController{
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView addUser ( @ModelAttribute("user") User user,
		      BindingResult result){
		try{
			userService.saveorupdate(user);
		}catch(Exception e)
		{}
		return new ModelAndView ("redirect:/users");
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView viewUsers() {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("users", userService.getAll() );

		return new ModelAndView ("user/list", model);
	}
	
	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public ModelAndView addArticle(@ModelAttribute("user") User user,
	      BindingResult result) {
	    return new ModelAndView("user/userform");
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
